class ShakesBoard::CommentsController < ActionController::Base


uses_component_template_root

	def index
		@comment_pages, @comments = paginate :comments, :per_page => 10
		@element_id=1
		@new_shake=0
	end


	  # GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html)
	  verify :method => :post, :only => [ :destroy, :create, :update ],
		 :redirect_to => { :action => :list }

	def list
	    @comment_pages, @comments = paginate :comments, :per_page => 10
	end

	def show
	    @comment = Comment.find(params[:id])
	end

	def new_comment
	    @comment = Comment.new
	end

	def create_comment
	    @comment = Comment.new(params[:comment])
		teacher_id=params[:other]['teacher_id']
		table=params[:other]['object']

	    if @comment.save
		    
		@comment.teacher << Teacher.find (teacher_id)
		    
		flash[:notice] = 'Comment was successfully created.'
	      redirect_to :action => 'list'
	    else
	      render :action => 'new'
	    end
	 end

	 def edit
	    @comment = Comment.find(params[:id])
	 end

	 def update
	    @comment = Comment.find(params[:id])
	    if @comment.update_attributes(params[:comment])
	      flash[:notice] = 'Comment was successfully updated.'
	      redirect_to :action => 'show', :id => @comment
	    else
	      render :action => 'edit'
	    end
	 end

	def destroy
	    Comment.find(params[:id]).destroy
	    redirect_to :action => 'list'
	 end
	  

	
	
	
	
	def _new_shake
		
		@comment = Comment.find(params[:comment_id])
		@comment.shakes+=1
		@comment.save
		@element_id=params[:ndiv]
		@new_shake=params[:new_shake]
	end	
	
	def show_opinions
		
		@comment_id=params[:comment_id]
		@window_id=params[:window_id]
		@opinion_pages, @opinions = paginate :opinions, :per_page => 100, :conditions => ['comment_id=?',@comment_id ],:order_by=>'karma desc'  


	end	
	
	
	def make_vote
		
		@opinion = Opinion.find(params[:opinion_id])
		@positive=params[:positive]
		@opinion.votes+=1
		if @positive=="1" 
			@opinion.karma+=6
		else
			@opinion.karma-=6
		end	
		@opinion.ip_vote.ip=request.remote_ip( )
		@opinion.save
		show_opinions
		render :partial =>'opinion_list'


	end	
	


  def create_opinion
    @opinions = Opinion.new(params[:opinion])
    if @opinions.save
      flash[:notice] = 'Opinions was successfully created.'
      #redirect_to :action => 'show_opinions', :comment_id=>params[:comment_id]	
	show_opinions
	render :partial =>'opinion_list'
    else
      render :action => 'new'
    end
  end

  def edit_opinion
    @opinions = Opinions.find(params[:id])
  end


  def destroy_opinion
    Opinions.find(params[:id]).destroy
    redirect_to :action => 'list'
  end
	
	
	  
end
